package com.quectel.util.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程名称工厂
 *
 * @author: cl
 * @email: louis.cao@quectel.com
 * @date: 2022-06-02 09:28:00
 */
public class NameThreadFactory implements ThreadFactory {
    private final AtomicInteger id = new AtomicInteger(0);

    private String namePrefix;

    public NameThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix;
    }

    @Override
    public Thread newThread(Runnable r) {
        String threadName = namePrefix + id.getAndIncrement();
        Thread thread = new Thread(r, threadName);
        thread.setDaemon(true);
        return thread;
    }
}
